package com.foot.travel.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.foot.travel.R;
import com.foot.travel.R.id;
import com.foot.travel.R.layout;
import com.koalac.ironman.common.module.ModuleActivity;
import com.koalac.ironman.net.pojo.request.SubmitOrder;
import com.koalac.ironman.net.pojo.response.SchoolTravelDetail;
import com.koalac.ironman.util.StringUtils;
import com.koalac.ironman.viewhelper.ViewInject;

public class SubmitOrderActivity extends ModuleActivity implements
	OnClickListener {

    public static final String SCHOOL_TRAVLE_DETAIL = "school_travel_detail";
    private SchoolTravelDetail mSchoolTravelDetail;

    @ViewInject(id = R.id.tour_num)
    EditText mTourNumView;
    @ViewInject(id = R.id.tour_date)
    TextView mTourDateView;
    @ViewInject(id = R.id.school)
    EditText mSchoolView;
    @ViewInject(id = R.id.class_mass)
    EditText mClassMassView;
    @ViewInject(id = R.id.linkman_name)
    EditText mLinkmanNameView;
    @ViewInject(id = R.id.linkman_mobile)
    EditText mLinkmanMobileView;
    @ViewInject(id = R.id.mail)
    EditText mMailView;
    @ViewInject(id = R.id.explain)
    EditText mExplainView;
    @ViewInject(id = R.id.submit_order_btn, click = "onClick")
    TextView mSubmitOrderView;

    private static final double UPPER_LIMIT_DEPOSIT = 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setNavigationTitle("订单详情");
	setBackButtonVisibility(true);
	mSchoolTravelDetail = (SchoolTravelDetail) getIntent()
		.getSerializableExtra(SCHOOL_TRAVLE_DETAIL);
	mTourDateView.setText(mSchoolTravelDetail.getDateline());
	setTourNumViewHint();

    }

    private void setTourNumViewHint() {
	if (mSchoolTravelDetail.getMaxFollowNumber() == 30) {
	    mTourNumView.setHint("人数范围在1到30");
	    return;
	}
	if (mSchoolTravelDetail.getMaxFollowNumber() == 49) {
	    mTourNumView.setHint("人数范围在31到49");
	    return;
	}
	if (mSchoolTravelDetail.getMaxFollowNumber() == Integer.MAX_VALUE) {
	    mTourNumView.setHint("人数在50人或以上");
	    return;
	}
    }

    private boolean checkInput() {
	if (StringUtils.isEmpty(mTourNumView.getText().toString())) {
	    toast("请填写出团人数！");
	    return false;
	}

	int tourNum = Integer.parseInt(mTourNumView.getText().toString());
	if (tourNum > mSchoolTravelDetail.getMaxFollowNumber()) {
	    toast("出团人数超出最大范围！");
	    return false;
	}
	if (tourNum <= 0) {
	    toast("出团人数不能为0！");
	    return false;
	}

	if (StringUtils.isEmpty(mSchoolView.getText().toString())) {
	    toast("请填写学校名称！");
	    return false;
	}
	if (StringUtils.isEmpty(mClassMassView.getText().toString())) {
	    toast("请填写班级 / 社团！");
	    return false;
	}
	if (StringUtils.isEmpty(mLinkmanNameView.getText().toString())) {
	    toast("请填写联系人姓名！");
	    return false;
	}
	if (StringUtils.isEmpty(mLinkmanMobileView.getText().toString())) {
	    toast("请填写联系人手机号码！");
	    return false;
	}
	if (!StringUtils.isMobilePhone(mLinkmanMobileView.getText().toString())) {
	    toast("联系人手机号码格式不对！");
	    return false;
	}
	if (StringUtils.isEmpty(mMailView.getText().toString())) {
	    toast("请填写电子邮箱！");
	    return false;
	}
	if (!StringUtils.isEmail(mMailView.getText().toString())) {
	    toast("电子邮箱格式不对！");
	    return false;
	}
//	if (StringUtils.isEmpty(mExplainView.getText().toString())) {
//		toast("请填写必要的说明！");
//		return false;
//	}
	return true;
    }

    @Override
    protected void initLayout(ViewGroup rootContentView) {
	addContentView(R.layout.activity_submit_order);
    }

    @Override
    public void onClick(View v) {
	int viewID = v.getId();
	switch (viewID) {
	case R.id.submit_order_btn:
	    if (checkInput()) {
		SubmitOrder submitOrder = new SubmitOrder();
		submitOrder.setProductId(mSchoolTravelDetail.getId());
		submitOrder.setProductName(mSchoolTravelDetail.getName());
		submitOrder
			.setProductFlag(mSchoolTravelDetail.getProductFlag());
		submitOrder.setNumber(mTourNumView.getText().toString());

		submitOrder.setStartDate(mSchoolTravelDetail.getDateline());
		submitOrder.setSchool(mSchoolView.getText().toString());

		submitOrder.setClassName(mClassMassView.getText().toString());
		submitOrder.setContactName(mLinkmanNameView.getText()
			.toString());
		submitOrder.setContactPhone(mLinkmanMobileView.getText()
			.toString());

		submitOrder.setEmail(mMailView.getText().toString());
		submitOrder.setRemark(mExplainView.getText().toString());
		submitOrder.setPerPrice(""
			+ mSchoolTravelDetail.getSelectPrice());

		int tourNum = Integer.parseInt(mTourNumView.getText()
			.toString());
		double totalPrice = mSchoolTravelDetail.getSelectPrice()
			* tourNum;
		submitOrder.setTotalPrice("" + totalPrice);

		if (totalPrice > UPPER_LIMIT_DEPOSIT) {
		    submitOrder.setFrontMoney("" + UPPER_LIMIT_DEPOSIT);
		} else {
		    submitOrder.setFrontMoney("" + totalPrice);
		}
		submitOrder.setPayWay("");
		Intent intent = new Intent(SubmitOrderActivity.this,
			PayOrderActivity.class);
		intent.putExtra(PayOrderActivity.SUBMIT_ORDER, submitOrder);
		intent.putExtra(SCHOOL_TRAVLE_DETAIL, mSchoolTravelDetail);
		startActivity(intent);
	    }
	    break;
	}
    }

}
